package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.*;
import com.yvtian.inv.dao.InvSupplementaryMaterialRequestMapper;
import com.yvtian.inv.service.InvSuMaterialRequestItemService;
import com.yvtian.inv.service.InvSupplementaryMaterialRequestService;
import com.yvtian.inv.service.ShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生产补领料单(inv_supplementary_material_request)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:44
 */
@Service("invSupplementaryMaterialRequestService")
public class InvSupplementaryMaterialRequestServiceImpl extends ServiceImpl<InvSupplementaryMaterialRequestMapper, InvSupplementaryMaterialRequestEntity> implements InvSupplementaryMaterialRequestService {

    @Autowired
    private InvSuMaterialRequestItemService invSuMaterialRequestItemService;
    @Autowired
    private ShopService shopService;

    @Override
    public IPage<InvSupplementaryMaterialRequestEntity> findByPage(Page<InvSupplementaryMaterialRequestEntity> page, InvSupplementaryMaterialRequestEntity invSupplementaryMaterialRequestEntity) {
        LambdaQueryWrapper<InvSupplementaryMaterialRequestEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvSupplementaryMaterialRequestEntity::getIsDel, 0);
        getQueryWrapper(invSupplementaryMaterialRequestEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvSupplementaryMaterialRequestEntity invSuMaterialRequest) {
        ArrayList<InvSuMaterialRequestItemEntity> itemList = invSuMaterialRequest.getInvSuMaterialRequestItemList();
        if (itemList != null && !itemList.isEmpty()) {
            LambdaUpdateWrapper<InvSuMaterialRequestItemEntity> itemUpdateWrapper = new LambdaUpdateWrapper<>();
            itemUpdateWrapper.eq(InvSuMaterialRequestItemEntity::getZcode, invSuMaterialRequest.getCode());
            invSuMaterialRequestItemService.remove(itemUpdateWrapper);
            invSuMaterialRequestItemService.saveBatch(itemList);
        }
        this.saveOrUpdate(invSuMaterialRequest);
        return true;
    }

    @Transactional
    @Override
    public boolean handleStorage(InvSupplementaryMaterialRequestEntity invSuEntity) {
        //查询子表领取的数量
        List<InvSuMaterialRequestItemEntity> itemEntityList = invSuMaterialRequestItemService.list(new LambdaQueryWrapper<InvSuMaterialRequestItemEntity>()
                .eq(InvSuMaterialRequestItemEntity::getZcode, invSuEntity.getCode()));
        for (InvSuMaterialRequestItemEntity suItem : itemEntityList) {
            if (suItem.getReceiveCount() == null || suItem.getReceiveCount().compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            //更新商品库存
            LambdaUpdateWrapper<ShopEntity> shopEntityWrapper = new LambdaUpdateWrapper<>();
            shopEntityWrapper.setSql("quantity = quantity - " + suItem.getReceiveCount());
            shopEntityWrapper.eq(ShopEntity::getShopCode, suItem.getShopCode());
            shopService.update(shopEntityWrapper);
        }

        //更新主表状态，（审核）
        LambdaUpdateWrapper<InvSupplementaryMaterialRequestEntity> materialRequestUpdateWrapper = new LambdaUpdateWrapper<>();
        materialRequestUpdateWrapper.eq(InvSupplementaryMaterialRequestEntity::getCode, invSuEntity.getCode());
        materialRequestUpdateWrapper.set(InvSupplementaryMaterialRequestEntity::getStatus, 1);
        materialRequestUpdateWrapper.set(InvSupplementaryMaterialRequestEntity::getCheckName, invSuEntity.getCheckName());
        materialRequestUpdateWrapper.set(InvSupplementaryMaterialRequestEntity::getCheckUser, invSuEntity.getCheckUser());
        materialRequestUpdateWrapper.set(InvSupplementaryMaterialRequestEntity::getCheckTime, new Date());
        this.update(materialRequestUpdateWrapper);
        return true;
    }

    private void getQueryWrapper(InvSupplementaryMaterialRequestEntity invSupplementaryMaterialRequestEntity, LambdaQueryWrapper<InvSupplementaryMaterialRequestEntity> queryWrapper) {
        queryWrapper
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getCode()), InvSupplementaryMaterialRequestEntity::getCode, invSupplementaryMaterialRequestEntity.getCode())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getIssuedUsername()), InvSupplementaryMaterialRequestEntity::getIssuedUsername, invSupplementaryMaterialRequestEntity.getIssuedUsername())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getIssuedAccount()), InvSupplementaryMaterialRequestEntity::getIssuedAccount, invSupplementaryMaterialRequestEntity.getIssuedAccount())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getIssuedUsername()), InvSupplementaryMaterialRequestEntity::getIssuedUsername, invSupplementaryMaterialRequestEntity.getIssuedUsername())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getIssuedAccount()), InvSupplementaryMaterialRequestEntity::getIssuedAccount, invSupplementaryMaterialRequestEntity.getIssuedAccount())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getReqDeptCode()), InvSupplementaryMaterialRequestEntity::getReqDeptCode, invSupplementaryMaterialRequestEntity.getReqDeptCode())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getReqDeptName()), InvSupplementaryMaterialRequestEntity::getReqDeptName, invSupplementaryMaterialRequestEntity.getReqDeptName())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getInvOrderCode()), InvSupplementaryMaterialRequestEntity::getInvOrderCode, invSupplementaryMaterialRequestEntity.getInvOrderCode())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getStockCode()), InvSupplementaryMaterialRequestEntity::getStockCode, invSupplementaryMaterialRequestEntity.getStockCode())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getStockName()), InvSupplementaryMaterialRequestEntity::getStockName, invSupplementaryMaterialRequestEntity.getStockName())
                .eq(invSupplementaryMaterialRequestEntity.getReqQuantity() != null, InvSupplementaryMaterialRequestEntity::getReqQuantity, invSupplementaryMaterialRequestEntity.getReqQuantity())
                .eq(invSupplementaryMaterialRequestEntity.getAmount() != null, InvSupplementaryMaterialRequestEntity::getAmount, invSupplementaryMaterialRequestEntity.getAmount())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getProjectCode()), InvSupplementaryMaterialRequestEntity::getProjectCode, invSupplementaryMaterialRequestEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getProjectName()), InvSupplementaryMaterialRequestEntity::getProjectName, invSupplementaryMaterialRequestEntity.getProjectName())
                .eq(invSupplementaryMaterialRequestEntity.getStatus() != null, InvSupplementaryMaterialRequestEntity::getStatus, invSupplementaryMaterialRequestEntity.getStatus())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getDelName()), InvSupplementaryMaterialRequestEntity::getDelName, invSupplementaryMaterialRequestEntity.getDelName())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getCreateName()), InvSupplementaryMaterialRequestEntity::getCreateName, invSupplementaryMaterialRequestEntity.getCreateName())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getCreateUser()), InvSupplementaryMaterialRequestEntity::getCreateUser, invSupplementaryMaterialRequestEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getUpdateName()), InvSupplementaryMaterialRequestEntity::getUpdateName, invSupplementaryMaterialRequestEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getUpdateUser()), InvSupplementaryMaterialRequestEntity::getUpdateUser, invSupplementaryMaterialRequestEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getRemarks()), InvSupplementaryMaterialRequestEntity::getRemarks, invSupplementaryMaterialRequestEntity.getRemarks())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getCheckName()), InvSupplementaryMaterialRequestEntity::getCheckName, invSupplementaryMaterialRequestEntity.getCheckName())
                .like(StringHelper.isNotEmpty(invSupplementaryMaterialRequestEntity.getCheckUser()), InvSupplementaryMaterialRequestEntity::getCheckUser, invSupplementaryMaterialRequestEntity.getCheckUser());
    }
}

